package com.gzw.dms.KJ;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.SelectDepartAdapter;
import com.gzw.dms.adapter.SelectJySuppliesAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.CuotomerInfo;
import com.gzw.dms.bean.JySuppliersInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectJySuppliesActivity extends BaseActivity {
    private Context mContext;
    private ListView lv_depart;
    private Button btn_query;
    private EditText et_depart;
    private List<JySuppliersInfo> ls;
    private SelectJySuppliesAdapter arr_adapter;
    private String tag = "SelectDepartActivity---->";
    private String type = "0";
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_select_jy_supplies);

    }


    @Override
    protected void initData() {
        mContext = this;
        mIntent = getIntent();
        type=(String)mIntent.getExtras().get("type");

    }

    public void initViews() {
        lv_depart = (ListView) findViewById(R.id.lv_depart);
        btn_query = (Button) findViewById(R.id.btn_query);
        et_depart = (EditText) findViewById(R.id.et_depart);
    }

    @Override
    protected void initEvents() {
        // TODO Auto-generated method stub
        btn_query.setOnClickListener(new MyOnClick());
        lv_depart.setOnItemClickListener(new myOnItemClickListener());
    }

    class MyOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            getDepartment();

        }
    }

    class myOnItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            Intent intent = new Intent();
            intent.putExtra("id", ls.get(position).getSuppliersId()
                    .toString());
            intent.putExtra("name", ls.get(position).getSuppliersName()
                    .toString());
            setResult(4, intent);
            finish();
        }
    }

    public void getDepartment() {
        if (isNull(et_depart)) {
            ShowOrderTask showOrderTask = new ShowOrderTask();
            showOrderTask.execute("");
        }

        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(et_depart.getText().toString().trim());
    }



    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        protected String doInBackground(String... params) {

            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("supplersName", params[0]);
            map.put("type",type);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appGetSupplersInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    ls = JsonUtil.getPersons(successMsg, new TypeToken<List<JySuppliersInfo>>() {
                    }.getType());
                    if (ls.size() == 0) {
                        T.showShort(mContext, "没有查到相关信息！");
                        arr_adapter = new SelectJySuppliesAdapter(mContext, ls);
                        lv_depart.setAdapter(arr_adapter);
                    } else {
                        arr_adapter = new SelectJySuppliesAdapter(mContext, ls);
                        lv_depart.setAdapter(arr_adapter);
                    }
                }
            }
        }
    }

}

